
;window.Teamlab = (function (Teamlab) {
  var
    isInit = false;

  var customEvents = {
    tasklistsPage : 'ontasklistspage',
    tasklistPage : 'ontasklistpage',
    taskPage : 'ontaskpage',

    tasklistsLoadTasks : 'ontasklistsloadtasks'
  };
  Teamlab.extendEventManager(customEvents);

  var CallbackMethods = {
    tasklists_load_task : function (params, task) {
      Teamlab.call(Teamlab.events.taskPage, window, [params, {pagemenu : Teamlab.getPageMenu('tasklists', 4), item : task}]);
    },

    tasklists_load_tasks : function (params, tasks) {
      var
        task = null,
        tasksInd = tasks.length;
      while (tasksInd--) {
        task = tasks[tasksInd];
        task.link = 'tasks/' + task.id;
      }
      Teamlab.call(Teamlab.events.tasklistsLoadTasks, window, [params, {items : tasks}]);
    },

    tasklists_load_selfprojects : function (params, projects) {
      var
        project = null,
        projectsInd = projects.length;
      while (projectsInd--) {
        project = projects[projectsInd];
        project.link = 'projects/' + project.id;
      }
      Teamlab.call(Teamlab.events.tasklistsPage, window, [params, {pagemenu : Teamlab.getPageMenu('tasklists', 4), items : projects}]);
    }
  };

  Teamlab.bind(Teamlab.events.tasklistsAnch, onTasklistsAnch);
  Teamlab.bind(Teamlab.events.taskAnch, onTasklistsTaskAnch);

  function onTasklistsAnch (params) {
    Teamlab.tasklistsLoadSelfProjects();
  }

  function onTasklistsTaskAnch (params, id) {
    Teamlab.tasklistsLoadTask(id);
  }

  Teamlab.tasklistsLoadSelfProjects = function () {
    return Teamlab.getPrjSelfProjects({___handler : 'tasklists-load-selfprojects', ___container : CallbackMethods});
  };

  Teamlab.tasklistsLoadTasksByProject = function (projectid) {
    return Teamlab.getPrjTasks({___handler : 'tasklists-load-tasks', ___container : CallbackMethods}, projectid);
  };

  Teamlab.tasklistsLoadTask = function (taskid) {
    return Teamlab.getPrjTask({___handler : 'tasklists-load-task', ___container : CallbackMethods}, taskid);
  };

  return Teamlab;
})(Teamlab);
